package nbcp.flux.feign

import feign.Client
import feign.codec.Decoder
import feign.codec.Encoder
import feign.optionals.OptionalDecoder
import org.springframework.beans.factory.ObjectFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.autoconfigure.http.HttpMessageConverters
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder
import org.springframework.cloud.openfeign.support.SpringDecoder
import org.springframework.cloud.openfeign.support.SpringEncoder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Conditional
import org.springframework.context.annotation.Configuration
import reactor.core.scheduler.Schedulers

@Configuration
class FeignGatewayConfig {

    @Bean
    fun feignEncoder(): Encoder {
        return SpringEncoder(feignHttpMessageConverter());
    }

    @Bean
    fun feignDecoder(): Decoder {
        return OptionalDecoder(
            ResponseEntityDecoder(SpringDecoder(feignHttpMessageConverter()))
        )
    }

    fun feignHttpMessageConverter(): ObjectFactory<HttpMessageConverters> {
        return ObjectFactory<HttpMessageConverters> {
            HttpMessageConverters(
                JsonHttpMessageConverter(),
                NormalTextHttpMessageConverter()
            )
        }
    }


}


